


package yf.mall.controller;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.yf.annotation.IgnoreAuth;
import com.yf.request.SearchParam;

import java.util.ArrayList;
import java.util.List;

import com.yf.utils.ShiroUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.dao.ShopLogRuleDao;
import yf.mall.dao.ShopLogTemDao;
import yf.mall.entity.ShopEntity;
import yf.mall.entity.ShopLogRuleEntity;
import yf.mall.entity.ShopLogTemEntity;
import yf.mall.entity.SysUsersEntity;
import yf.mall.service.ShopLogRuleService;
import yf.mall.service.ShopService;
import yf.mall.service.SysUsersService;
import yf.mall.vo.ShopLogRuleVo;
import yf.mall.vo.ShopLogTemVo;
import yf.mall.service.ShopLogTemService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * 店铺物流模版Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Aug 19 17:02:10 CST 2020
 */
@RestController
@RequestMapping("/shop/log/tem")
public class ShopLogTemController {
    @Autowired
    private ShopLogTemService shopLogTemService;

    @Autowired
    private ShopLogRuleDao shopLogRuleDao;

    /**
     * 列表店铺物流模版
     */
    @SysLog("查看店铺物流模版")
    @RequestMapping("/list")
    @RequiresPermissions("shop:log:tem:list")
    public R list(@RequestBody SearchParam<ShopLogTemVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<ShopLogTemVo> list = shopLogTemService.findVo(params.getEntity());
        // 组装模版规格
        list.forEach(shopLogTemVo -> {
            List<ShopLogRuleEntity> shopLogRules = this.shopLogRuleDao.findByTemId(shopLogTemVo.getShopLogTem().getId());
            shopLogTemVo.setShopLogRules(shopLogRules);
        });
        return R.ok(new PageInfo(list));
    }

    /**
     * 下载订单
     */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("shop:log:tem:list")
    public void down(@RequestBody SearchParam<ShopLogTemVo> params, HttpServletResponse response) {
        List<ShopLogTemVo> list = shopLogTemService.findVo(params.getEntity());
        shopLogTemService.downLoad(response, list);
    }


    /**
     * 查看店铺物流模版
     */
    @SysLog("查看店铺物流模版")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("shop:log:tem:info")
    public R info(@PathVariable("id") Long id) {
        ShopLogTemVo vo = shopLogTemService.getVo(id);
        List<ShopLogRuleEntity> shopLogRules = this.shopLogRuleDao.findByTemId(vo.getShopLogTem().getId());
        vo.setShopLogRules(shopLogRules);
        return R.ok(vo);
    }
    @Autowired
    private ShopLogTemDao shopLogTemDao;
    /**
     * 保存店铺物流模版
     */
    @SysLog("保存店铺物流模版")
    @RequestMapping("/save")
    @IgnoreAuth
//    @RequiresPermissions("shop:log:tem:save")
    public R save(@RequestBody ShopLogTemVo vo) {
        shopLogTemService.saveVo(vo);
        return R.ok(vo);
    }
    /**
     * 修改店铺物流模版
     */
    @SysLog("修改店铺物流模版")
    @RequestMapping("/update")
    @RequiresPermissions("shop:log:tem:update")
    public R update(@RequestBody ShopLogTemVo vo) {
        shopLogTemService.updateVo(vo);
        return R.ok(vo);
    }


    /**
     * 删除店铺物流模版
     */
    @SysLog("删除店铺物流模版")
    @RequestMapping("/delete")
    @RequiresPermissions("shop:log:tem:delete")
    public R delete(Long[] ids) {
        shopLogTemService.deleteBatch(ids);
        for (Long id : ids) {
            this.shopLogRuleDao.deleteByLogId(id);
        }
        return R.ok();
    }

    /**
     * 所有店铺物流模版
     */
    @SysLog("所有店铺物流模版")
    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order != null ? order : "id desc");
        List<ShopLogTemVo> list = shopLogTemService.findVo(null);
        list.forEach(shopLogTemVo -> {
            List<ShopLogRuleEntity> shopLogRules = this.shopLogRuleDao.findByTemId(shopLogTemVo.getShopLogTem().getId());
            shopLogTemVo.setShopLogRules(shopLogRules);
        });
        return R.ok(list);
    }

    /**
     * 所有enums
     */
    @SysLog("所有enums")
    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String, Object> options = new HashMap();
        return R.ok(options);
    }


    /***********自定义开始***********/
    @Autowired
    private SysUsersService sysUsersService;
    @Autowired
    private ShopService shopService;
    /**
     * 模板列表
     */
    @RequestMapping("/getList")
    public R getList() {
        //获取店铺id
        Long sysUser_id = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sysUser_id);
        Integer status = null;
        Long shopId = null;
        //获取该店铺的运费设置
        if (userId != null){
            ShopEntity shopEntity = shopService.findByUserId(userId);
            shopId = shopEntity.getId();
            status = shopLogTemService.getStatus(shopEntity.getId());
        }
        List<ShopLogTemEntity> list = shopLogTemService.getList(shopId);
        return R.ok(list).putExtra("status",status);
    }
    /** que
     * 所有模板规则
     */
    @RequestMapping("/getLogRuleList")
    public R getLogRuleList() {
        //获取店铺id
        Long sysUser_id = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sysUser_id);
        List<ShopLogRuleEntity> list = new ArrayList<>();
        //获取该店铺的运费设置
        if (userId != null){
            //获取店铺模板
            ShopEntity shopEntity = shopService.findByUserId(userId);
            List<ShopLogTemEntity> temEntityList = shopLogTemDao.findByShopId(shopEntity.getId());
            //获取模板规则
            if (temEntityList != null && temEntityList.size() > 0){
                for (ShopLogTemEntity shopLogTemEntity : temEntityList) {
                    List<ShopLogRuleEntity> ruleEntityList = shopLogRuleDao.findByTemId(shopLogTemEntity.getId());
                    list.addAll(ruleEntityList);
                }
            }
        }
        return R.ok(list);
    }
    /** que
     * 保存或修改店铺运费设置
     */
    @RequestMapping("/savaOrUpdate/{status}")
    public R savaOrUpdate(@PathVariable("status")Integer status) {
        //获取店铺id
        Long sysUser_id = ShiroUtils.getUserId();
        Long userId = sysUsersService.findBySysUserId(sysUser_id);
        //获取该店铺的运费设置
        if (userId != null){
            //获取店铺
            ShopEntity shopEntity = shopService.findByUserId(userId);
            Integer shopStatus = shopLogTemService.getStatus(shopEntity.getId());
            if (shopStatus == null){
                 shopLogTemDao.saveShopStatus(shopEntity.getId(),status);
            }else {
                shopLogTemDao.updateByShopId(shopEntity.getId(), status);
            }
        }
        return R.ok();
    }
}
